package action.contract;

import java.util.List;

import model.TContract;
import model.contract.Contractcheck;
import model.contract.Contractensure;
import model.contract.Contractmortgage;
import model.contract.Contractpledge;
import model.contract.Loangrant;
import service.contract.ContractCheckService;
import service.contract.ContractService;
import service.contract.ShowContractService;
import util.Time;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

@SuppressWarnings("serial")
public class ContractCheckAction extends BaseAction{
	private ContractCheckService contractCheckService;
	private Contractcheck contractcheck;
	private List checklist;
	private ContractService contractService;
	private ShowContractService showContractService;
		
	public String execute(){
		checklist=contractCheckService.checkList();
		return "success";
	}
	
	public String save() throws Exceptions{
		contractcheck.setChecktime(Time.CurrentTimestamp());
		contractcheck.setUserid(Global.userid());
		if(contractcheck.getOpinion().equals("2")){
			TContract contractloan=showContractService.get_ht(contractcheck.getContractkey());
			contractloan.setContractstate(2);
			contractService.save_in_contract(contractloan);
			if(showContractService.getCount(3,contractloan.getContractId())!=0){
			Contractensure contractEnsure=showContractService.get_htEnsure("contractloanid",contractloan.getContractId());
			contractEnsure.setState("2");
			contractService.save_in_contractensure(contractEnsure);
			}
			if(showContractService.getCount(1, contractloan.getContractId())!=0){
			Contractmortgage contractMortgage=showContractService.get_htMortgage("contractloanid",contractloan.getContractId());
			contractMortgage.setState("2");
			contractService.save_in_contractmortgage(contractMortgage);
			}
			if(showContractService.getCount(2,contractloan.getContractId())!=0){
			Contractpledge contractPledge=showContractService.get_htPledge("contractloanid",contractloan.getContractId());
			contractPledge.setState("2");
			contractService.save_in_contractpledge(contractPledge);
			}
			//===分发===//
			List<Loangrant> list=showContractService.get_loangrant("contractid="+contractcheck.getContractkey()+" and state=10");
			for(Loangrant loangrant:list){
				loangrant.setState(9);
				contractService.save_in_loangrant(loangrant);
			}
		}
		if(contractcheck.getOpinion().equals("1")){
			TContract contractloan=showContractService.get_ht(contractcheck.getContractkey());
			contractloan.setContractstate(1);
			contractService.save_in_contract(contractloan);
			if(showContractService.getCount(3,contractloan.getContractId())!=0){
			Contractensure contractEnsure=showContractService.get_htEnsure("contractloanid",contractloan.getContractId());
			contractEnsure.setState("1");
			contractService.save_in_contractensure(contractEnsure);
			}
			if(showContractService.getCount(1, contractloan.getContractId())!=0){
			Contractmortgage contractMortgage=showContractService.get_htMortgage("contractloanid",contractloan.getContractId());
			contractMortgage.setState("1");
			contractService.save_in_contractmortgage(contractMortgage);
			}
			if(showContractService.getCount(2,contractloan.getContractId())!=0){
			Contractpledge contractPledge=showContractService.get_htPledge("contractloanid",contractloan.getContractId());
			contractPledge.setState("1");
			contractService.save_in_contractpledge(contractPledge);
			}
			//===分发===//
			List<Loangrant> list=showContractService.get_loangrant("contractid="+contractcheck.getContractkey()+" and state=10");
			if(list!=null){
			for(Loangrant loangrant:list){
				loangrant.setState(100);
				contractService.save_in_loangrant(loangrant);
			}
			}
		}
		String msg=contractCheckService.contractCheckSave(contractcheck);
		MessageJump(msg,"contractCheck");
		return "message";
	}

	public Contractcheck getContractcheck() {
		return contractcheck;
	}

	public void setContractcheck(Contractcheck contractcheck) {
		this.contractcheck = contractcheck;
	}

	public ContractCheckService getContractCheckService() {
		return contractCheckService;
	}

	public void setContractCheckService(ContractCheckService contractCheckService) {
		this.contractCheckService = contractCheckService;
	}


	public List getChecklist() {
		return checklist;
	}


	public void setChecklist(List checklist) {
		this.checklist = checklist;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public ShowContractService getShowContractService() {
		return showContractService;
	}

	public void setShowContractService(ShowContractService showContractService) {
		this.showContractService = showContractService;
	}




}
